package cn.tedu.egaddress0805.Controller;

import cn.tedu.egaddress0805.mapper.AddressMapper;
import cn.tedu.egaddress0805.pojo.dto.AddressAddParam;
import cn.tedu.egaddress0805.pojo.enity.Address;
import cn.tedu.egaddress0805.pojo.vo.AddressInfoVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.aop.AopAutoConfiguration;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/v1/address")
public class AddressController {
    @Autowired
    AddressMapper addressMapper;
    @Autowired
    private AopAutoConfiguration aopAutoConfiguration;

    @PostMapping("add")
    public String insertAddress(@RequestBody AddressAddParam addressAddParam){
        System.out.println("地址："+addressAddParam);
        Address address = new Address();
        BeanUtils.copyProperties(addressAddParam,address);
        System.out.println("地址："+address);
        int row=addressMapper.insert(address);
        System.out.println("受影响行数："+row);
        return row>0?"添加地址成功":"添加地址失败";
    }

    @RequestMapping("list")
    public List<AddressInfoVO> listAddress(){
        List<AddressInfoVO> list=addressMapper. listAddress();
        return list;
    }

    @PostMapping("del/{id}")
    public String deleteAddress(@PathVariable("id") Integer id){
        System.out.println("id:"+id);
        int row=addressMapper.deleteAddress(id);
        System.out.println("受影响行数："+row);
        return row>0?"删除成功":"删除失败";
    }
}
